package com.jingdianjichi.subject.domain.service.impl;

import com.jingdianjichi.subject.common.enums.CategoryTypeEnum;
import com.jingdianjichi.subject.common.enums.IsDeletedEnum;
import com.jingdianjichi.subject.domain.convert.SubjectLabelConverter;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    public SubjectLabelService subjectLabelService;

    @Resource
    public SubjectMappingService subjectMappingService;

    @Resource
    public SubjectCategoryService subjectCategoryService;

    @Override
    public void insert(SubjectLabelBO subjectLabelBO) {
        subjectLabelBO.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        subjectLabelService.insert(SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO));
    }

    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {

        int k=subjectLabelService.update(SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO));
        if(k==1){
            return true;
        }
        return false;
    }

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel=SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedEnum.DELETED.getCode());
        int k= subjectLabelService.update(subjectLabel);
        if(k>0){
            return true;
        }
        return false;
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
        if(subjectCategory.getCategoryType()== CategoryTypeEnum.PRIMARY.getCode()){
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
            List<SubjectLabel> subjectLabelList=subjectLabelService.queryByCondition(subjectLabel);
            return SubjectLabelConverter.INSTANCE.convertCategoryListToCategoryListBo(subjectLabelList);
        }
        Long categoryId=subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping=new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        List<SubjectMapping> subjectMappingList=subjectMappingService.queryLabelId(subjectMapping);
        if(CollectionUtils.isEmpty(subjectMappingList)){
            return Collections.emptyList();
        }
        List<Long> labelIdList=new ArrayList<>();
        for(int i=0;i<subjectMappingList.size();i++){
            labelIdList.add(subjectMappingList.get(i).getLabelId());
        }
        List<SubjectLabel> subjectLabelList=subjectLabelService.batchQueryById(labelIdList);
        return SubjectLabelConverter.INSTANCE.convertCategoryListToCategoryListBo(subjectLabelList);
    }
}
